package com.leo.controller;

import com.leo.base.exception.result.R;
import com.leo.base.mail.service.IMailService;
import com.leo.base.mail.vo.Mail;
import com.leo.base.util.FileUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MailTestController
 * @Description
 * @Author wangss
 * @date 2020.09.23 20:03
 * @Version 1.0
 */
@RestController
@RequestMapping("/mail")
public class MailTestController {

    @Resource
    private IMailService mailService;

    @Resource
    private FileUtil fileUtil;

    @RequestMapping("/sendHtmlMail")
    public void sendHtmlMail() {
        Mail mail = new Mail();
        mail.setFrom("18616345762@163.com");
        mail.setTo("wangss_kf@163.com");
        mail.setSubject("这是一封测试邮件");
        mail.setText("<h1>你好，这是一封来自你朋友的测试邮件</h1>");
        mail.setHtml(true);
        mailService.sendMail(mail);
    }

    @RequestMapping("/sendTemplateMail")
    public R sendTemplateMail(@RequestParam(value = "multipartFiles", required = false) MultipartFile[] multipartFiles) {
        Mail mail = new Mail();
        mail.setFrom("18616345762@163.com");
        mail.setTo("wangss_kf@163.com");
        mail.setSubject("这是一封测试邮件");

        Map<String, Object> map = new HashMap<>(2);
        map.put("username", "12345");
        map.put("templateType", "45678");
        mail.setVariables(map);
        File[] files = fileUtil.cacheFiles(multipartFiles);
        if (ArrayUtils.isNotEmpty(files)) {
            mail.setFiles(files);
        }

        mailService.sendMail(mail);

        return R.ok();
    }
}
